package com.rzico.sbl.ui.report.driver

import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.DriverStation
import com.rzico.sbl.viewmodel.DriverViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class DriverStationPledgeActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<DriverViewModel>()

    private lateinit var mShopId: String

    override fun initData() {
        initTitle(title = "押桶")

        initLayout()

        mShopId = intent.getExtra("shopId")

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关押桶信息！"
                }

                recycleList.also { view ->
                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData() },
                    )

                    view.addItemDecoration(SpaceTBDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<DriverStation>(R.layout.item_report_station_pledge) { jector, bean, _ ->
                            jector.apply {
                                text(
                                    R.id.item_pledge_name,
                                    "${bean.barrel?.name}-${bean.barrel?.id}"
                                )
                                text(R.id.item_pledge_num, bean.mortgage.orEmpty("0"))
                                text(R.id.item_pledge_money, bean.pledge.formatDecimal())

                                clicked {
                                    startActivityEx<DriverStationBuyRecordActivity>(
                                        "title" to "押桶记录",
                                        "type" to "1",
                                        "shopId" to mShopId,
                                        "barrelId" to bean.barrel?.id,
                                        "dateMode" to "0"
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    /**
     * 水站押桶品牌列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .pledgeList(
                shopId = mShopId,
                onSuccess = {
                    mList.clear()
                    mList.addItems(it)

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}